package com.neouton.common.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ReportUtils {


    /**
     * 基于数据模板导出excel
     * @param response
     * @param workbook
     * @param fileName
     * @throws IOException
     */
    public static void exportExcel(TemplateExportParams templatePath, Map<String, Object> map, String fileName, HttpServletResponse response) throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(templatePath, map);
        downLoadExcel(fileName, response, workbook,"新邮通内部资料");
    }

    /**
     * 非模板导出excel
     * @param params
     * @param pojoClass
     * @param list
     * @throws IOException
     */
    public static void exportNoTempExcel(ExportParams params, Class<?> pojoClass, List<?> list,HttpServletResponse response) throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, list);
        downLoadExcel("aaa", response, workbook,"新邮通内部资料");
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook,String text) throws Exception {
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        byte[] bytes = text.getBytes();
        text = new String(bytes, StandardCharsets.UTF_8);
        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(response.getOutputStream());
        BufferedImage bufferedImage = ExcelWaterRemarkUtils.createTransparentImage(bufferedOutPut,text);//fontPath 字体文件所在路径
        int sheets = workbook.getNumberOfSheets();
        //循环sheet给每个sheet添加水印
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            //获取excel实际所占行
            int row = sheet.getFirstRowNum() + sheet.getLastRowNum();
            //获取excel实际所占列
            int cell = sheet.getRow(sheet.getFirstRowNum()).getLastCellNum() + 1;
            //根据行与列计算实际所需多少水印
            ExcelWaterRemarkUtils.putWaterRemarkToExcel(workbook, sheet, bufferedImage, 0, 0, 5, 5, cell / 5 + 1, row / 5 + 1, 0, 0);
//            //excel加密只读
//            sheet.protectSheet(UUID.randomUUID().toString());
        }
        workbook.write(bufferedOutPut);
        bufferedOutPut.flush();
        bufferedOutPut.close();
    }

    /**
     * 校验空数据，返回新集合，主要easypoi在识别数据时会识别空数据
     */
    public static List removeNullObject(List list){
        ArrayList newList = new ArrayList<>();
        for ( Object obj:list){
            if(!ObjectUtils.isAllFieldNull(obj)){
                newList.add(obj);
            }
        }
        return newList;
    }

    /**
     * 导入Excel,识别数据，并清除误识别行
     * @param inputstream
     * @param pojoClass
     * @param params
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> importExcel(InputStream inputstream, Class<?> pojoClass,
                                          ImportParams params) throws Exception {
        List<T> list = ExcelImportUtil.importExcel(inputstream, pojoClass, params);
        return removeNullObject(list);
    }
}
